﻿//条目基本信息
packageContent.itemActionBase = function(actionItemId) {
	var _actionItemId = actionItemId;
	var _initActionBaseInfo = function() {
		//基本信息赋值
		if ((pack.getActionItemMap() == undefined)
				|| (pack.getActionItemMap().get(_actionItemId) == null)) {
			return;
		}

		var actionItem = pack.getActionItemMap().get(_actionItemId);
		if ("Y" == actionItem.getIsVisible()) {
			$("#p_actionBase_isShow").attr("checked",true);
		}
		if ("Y" == actionItem.getIsMust()) {
			$("#p_actionBase_isMust").attr("checked",true);
		}
		$("#p_actionBase_isShow").attr("checked") ? "Y" : "N";
		$("#p_actionBase_desc").val(actionItem.getDescription());
		$("#p_actionBase_seq").val(actionItem.getSeqNumber());
		
		if (Validate.isNull(actionItem.getSeqNumber())) {
			//序列的初始化
			var fistItemId = "#pageBody tr[id^='"+_actionItemId.substring(0, _actionItemId.lastIndexOf("_"))+"_']";
			var partObjCD = packageContent.getFirstPartObjCd(fistItemId,"id")
			$("#p_actionBase_seq").val(packageContent.getMaxIndex(MAXTYPE.actionItem,partObjCD));
		}
		
		
	};
	
	var _initButton = function() {
		$("#p_actionBase_save").bind("click", function() {
			if(!_saveActionBaseInfo()) {
				return ;
			}
			_doClose()
		});
		$("#p_actionBase_cancel").bind("click", function() {
			_doClose();
		});
		$("#p_actionBase_auto").bind("click", function() {
			_doAuto(_actionItemId);
		});

		$("#p_actionBase_save").addClass("btn-blue");
		$("#p_actionBase_cancel").addClass("btn-gray");
	}

	var _doAuto = function(_actionItemId){
		//基本信息赋值
		if ((pack.getActionItemMap() == undefined)
				|| (pack.getActionItemMap().get(_actionItemId) == null)) {
			alert("当【参与对象】,【动作】,【目标对象】均设置时，可自动设置");
			return;
		}

		var actionItem = pack.getActionItemMap().get(_actionItemId);
		
		if(!actionItem.getPartobjNumber() || !actionItem.getEffectobjNumber() || 
				!actionItem.getActionTypeCd()) {
			alert("当【参与对象】,【动作】,【目标对象】均设置时，可自动设置");
			return;
		}	
		
		var actionTypeSpan = packageContent.getActionTypeSpan(_actionItemId);
		var effectObjSpan = packageContent.getEffectObjSpan(_actionItemId);
		var effectobjNumber = actionItem.getEffectobjNumber();
		var effectObj = pack.getPackObjMap().get(effectobjNumber);
		
		var _actionItemId = "tr[id^='" + _actionItemId.substring(0, _actionItemId.lastIndexOf("_"))+"_']";
		var partObjSpan = packageContent.getFirstPartObjSpan(_actionItemId);
		
		var desc;
		//【新装】动作，显示[参与对象][动作]
		if (ACTION.TYPE.NEW == actionTypeSpan.attr('actiontype')) {
			desc = ((partObjSpan.html().indexOf('[') == -1) ? partObjSpan.html() : partObjSpan.html().substr(4))
			                +"【"+actionTypeSpan.html()+"】";
		} else {
			desc = ((partObjSpan.html().indexOf('[') == -1) ? partObjSpan.html() : partObjSpan.html().substr(4))
						+"【"+actionTypeSpan.html()+"】"+effectObj.getObjectName();
		}
		$("#p_actionBase_desc").val(desc);
		
	}
	
	var _validate = function() {
		var itemActionTd = $("#" + _actionItemId + " td")[1];
		var actionDesc = $("#p_actionBase_desc").val();
		if (!actionDesc){ 
			alert("描述不能为空");
			return false;
		} else {
			if (actionDesc.length > 100) {
				alert("描述不能大于100");
				return false;
			}
		}
		
		if (Validate.isNull($("#p_actionBase_seq").val())) {
			PACK.ALERT.warn("请设置【动作条目】顺序！");
			return false;
		}
		if ((parseInt($("#p_actionBase_seq").val()) != $("#p_actionBase_seq").val()) || $("#p_actionBase_seq").val() < 1) {
			PACK.ALERT.warn("【动作条目】序号需为大于1的数字");
			return false;
		}
		
		return true;
	}
	//保存
	var _saveActionBaseInfo = function() {
		//保存前的校验
		if(!_validate()){
			return false; 
		}
		var actionBaseDesc = $("#p_actionBase_desc").val();
		var isShow = $("#p_actionBase_isShow").attr("checked") ? "Y" : "N";
		var isMust = $("#p_actionBase_isMust").attr("checked") ? "Y" : "N";
		var actionItemSeq = $("#p_actionBase_seq").val()
		
		//重新赋值
		var _packActionItem;
		if (!(pack.getActionItemMap() == undefined)
				&& !(pack.getActionItemMap().get(_actionItemId) == null)) {
			_packActionItem = pack.getActionItemMap().get(_actionItemId);
		} else {
			_packActionItem = new PackageActionItem();
		}

		_packActionItem.setActionItemId(_actionItemId);
		_packActionItem.setDescription(actionBaseDesc);
		_packActionItem.setIsMust(isMust);
		_packActionItem.setIsVisible(isShow);
		_packActionItem.setSeqNumber(actionItemSeq);
		
		var actionItemMap;
		if (pack.getActionItemMap() == undefined) {
			actionItemMap = new HashMap();
		} else {
			actionItemMap = pack.getActionItemMap();
		}
		actionItemMap.put(actionItemId, _packActionItem);

		pack.setActionItemMap(actionItemMap);

		//整体逻辑 ：第一列 第4个
		var itemActionTd = $("#" + actionItemId + " td")[3];
		
		$(itemActionTd).find("span").attr("title",actionBaseDesc);
		if(actionBaseDesc.length > 40) {
			actionBaseDesc = actionBaseDesc.substring(0,45)+"...";
		}
		
		var showName = '['+actionItemSeq+'] '+actionBaseDesc;
		$(itemActionTd).find("span").html(showName);
		
			
		return true;
	}

	//关闭
	var _doClose = function() {
		$("#openActionBaseInfoPage").window('close');
	}

	//初始化包参与对象
	var _init = function() {
		_initActionBaseInfo();
		_initButton();
	}

	_init();
}
